package com.dacd.pdf.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dacd.adapter.PdfFileAdapter;
import com.dacd.filehelper.FileConstant;
import com.dacd.filehelper.FileUtils;
import com.dacd.pdf.R;

import java.io.File;

public class PdfFileScamperActivity extends BaseActivity {

    private RecyclerView pdfListRv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_pdf_file_scamper);
        init();
        initData();
    }

    private void init() {
        pdfListRv = (RecyclerView) findViewById(R.id.apfs_rv);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pdfListRv.addItemDecoration(new DividerItemDecoration(
                this, DividerItemDecoration.VERTICAL));
        pdfListRv.setLayoutManager(layoutManager);
    }

    private void initData() {
        String sdPath = FileUtils.getSDPath();
        if (sdPath == null) {//没有SD卡不能保存，可以添加提示
            return;
        }
        final File folderFile = new File(sdPath + FileConstant.PDF_PATH);
        if (!folderFile.exists() || !folderFile.isDirectory()) {
            return;
        }
        final File[] allPdfFiles = folderFile.listFiles();
        if (allPdfFiles == null || allPdfFiles.length < 1) {
            return;
        }
        PdfFileAdapter pdfFileAdapter = new PdfFileAdapter(allPdfFiles);
        pdfListRv.setAdapter(pdfFileAdapter);
        pdfFileAdapter.setOnItemClickLitener(new PdfFileAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(PdfFileScamperActivity.this, ShowEditedPdfActivity.class);
                intent.putExtra("fileName", allPdfFiles[position].getName());
                startActivity(intent);
            }
        });
    }

}
